package com.qipay.admin.utils;

import com.qipay.admin.cp.vo.CpInfoDTO;
import com.qipay.admin.withdraw.service.WithdrawLogicService;
import com.qipay.admin.withdraw.vo.WithdrawVo;
import com.qipay.baas.model.Cp;
import com.qipay.baas.model.CpAccount;
import com.qipay.baas.model.CpInfo;
import com.qipay.baas.model.CpWithdrawTradlog;
import com.qipay.cp.CpService;
import com.qipay.cpaccount.CpAccountService;
import com.qipay.cpinfo.service.CpInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class CpInfoBuilder {

    @Autowired
    CpAccountService cpAccountService;

    @Autowired
    CpService cpService;

    @Autowired
    CpInfoService cpInfoService;

    @Autowired
    WithdrawLogicService withdrawLogicService;

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public List<CpInfoDTO> buildCpInfoCTOByCpInfo(List<CpInfo> lst, boolean needAccount, boolean needCp) {

        if (lst != null) {
            List<CpInfoDTO> cpInfoDTOs = new ArrayList<>(lst.size());
//            for(CpInfo db : lst){
//                cpInfoDTOs.add(buildCpInfoCTO(db));
//            }
            logger.error("cpInfoDTOs:" + cpInfoDTOs);
            lst.forEach(t -> cpInfoDTOs.add(buildCpInfoCTOByCpInfo(t, needAccount, needCp)));
            return cpInfoDTOs;
        }
        return null;
    }

    public CpInfoDTO buildCpInfoCTOByCpInfo(CpInfo cpInfo, boolean needAccount, boolean needCp) {
        if (cpInfo != null) {
            CpInfoDTO dto = new CpInfoDTO();
            BeanUtils.copyProperties(cpInfo, dto);
            if (needAccount) {
                CpAccount cpAccount = cpAccountService.get(cpInfo.getCpId());
                if (cpAccount != null && cpAccount.getCpId() > 0) {
                    dto.setCpAccount(cpAccount);
                }
            }
            if (needCp) {
                Cp cp = cpService.getOneById(cpInfo.getCpId());
                if (cp != null && cp.getId() != null) {
                    cp.setFeeDefaultRecharge(cp.getFeeDefaultRecharge().multiply(SiteHelper.decimalFor100));
                    cp.setFeeWithdrawAdvince(cp.getFeeWithdrawAdvince().multiply(SiteHelper.decimalFor100));
                    dto.setCp(cp);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    dto.setCpCreateTime(sdf.format(cp.getCreateTime()));
                }
            }
            //提现记录
            Byte state = 2;
            if (cpInfo!=null){
                Long totalAmount = 0l;
                BigDecimal fee = new BigDecimal(0.0);
                List<CpWithdrawTradlog>cpWithdrawTradlogs = withdrawLogicService.listByCpId(cpInfo.getCpId(),state);
                if (cpWithdrawTradlogs!=null&&cpWithdrawTradlogs.size()>0){
                    for(CpWithdrawTradlog wv:cpWithdrawTradlogs){
                        totalAmount = totalAmount + wv.getAmount();
                        fee = fee.add( wv.getFee());
                    }
                }
                dto.setTotalWidthdraw(totalAmount);
                dto.setTotalFee(fee);
            }

            return dto;
        }
        return null;
    }

    public List<CpInfoDTO> buildCpInfoCTOByCp(List<Cp> lst, boolean needAccount, boolean needCp) {
        if (lst != null) {
            List<CpInfoDTO> cpInfoDTOs = new ArrayList<>(lst.size());
            lst.forEach(t -> cpInfoDTOs.add(buildCpInfoCTOByCp(t, needAccount, needCp)));
            return cpInfoDTOs;
        }
        return null;
    }

    public CpInfoDTO buildCpInfoCTOByCp(Cp cp, boolean needAccount, boolean needCp) {
        if (cp != null && cp.getId() != null) {
            cp.setFeeDefaultRecharge(cp.getFeeDefaultRecharge().multiply(SiteHelper.decimalFor100));
            cp.setFeeWithdrawAdvince(cp.getFeeWithdrawAdvince().multiply(SiteHelper.decimalFor100));
            CpInfoDTO dto = new CpInfoDTO();
            dto.setCp(cp);
            //BeanUtils.copyProperties(cpInfo, dto);
            if (needAccount) {
                CpAccount cpAccount = cpAccountService.get(cp.getId());
                if (cpAccount != null && cpAccount.getCpId() > 0) {
                    dto.setCpAccount(cpAccount);
                }
            }
            if (needCp) {
                CpInfo cpInfo = cpInfoService.get(cp.getId());
                if (cpInfo != null && cpInfo.getCpId() != null) {
                    BeanUtils.copyProperties(cpInfo, dto);
                }
            }

            return dto;
        }
        return null;
    }
}
